package com.kk.service;

import com.kk.model.Order;
import lombok.RequiredArgsConstructor;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;

/**
 * the type of springboot-drools-demo
 *
 * <p>
 * .
 *
 * @author 铁甲小宝
 * @date 2023-06-18
 */
@Service
@RequiredArgsConstructor
public class DroolsService {

    private final KieBase kieBase;

    public String getScore(Integer price) {
        Order order = new Order();
        order.setPrice(price);
        KieSession kieSession = kieBase.newKieSession();
        kieSession.insert(order);
        kieSession.fireAllRules();
        kieSession.dispose();

        Integer score = order.getScore();
        StringBuilder builder = new StringBuilder();
        builder.append("消费 ")
                .append(price)
                .append(" 元, ")
                .append("积 ");

        if (null == score){
            builder.append("未定义该分段的规则");
        } else {
            builder.append(score)
                    .append(" 分.");
        }

        return builder.toString();
    }
}
